﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UnityScriptEngine;

namespace MBB_Payroll_Terminal
{
    public partial class frmShowDTR : Form
    {
        UnityProcessor proc = new UnityProcessor();
        Threader t = new Threader();
        UnityScript dtr = new UnityScript("DTR", 5);
        DataTable emp = new DataTable();
        Timer tmr = new Timer();
        string curID, path;
        public frmShowDTR(string id)
        {
            curID = id;
            InitializeComponent();
            InitializeFrameSliderForm();
        }
        private void InitializeForm()
        {
            tmr.Interval = 10;
            tmr.Enabled = true;
            proc.ProcessCompleted += new ProcessHandler(proc_ProcessCompleted);
            proc.ProcessError += new ProcessHandler(proc_ProcessError);
            tmr.Tick += new EventHandler(delegate
            {
                if (btnClose.Enabled) btnClose.Focus();
            });
            t.MethodCompleted += new EventHandler(t_MethodCompleted);

            t.MethodCall(GetDTR);
        }

        void t_MethodCompleted(object sender, EventArgs e)
        {
            t.SafeCall(this, delegate
            {
                switch (sender.ToString())
                {
                    case "GetEmpDtr":
                        grdLst.DataSource = emp;
                        grdLst.Sort(grdLst.Columns[0], ListSortDirection.Descending);
                        btnClose.Focus();
                        break;
                }
            });
        }
        void proc_ProcessError(string id, string desc)
        {
            t.SafeCall(this, delegate
            {
                MessageBox.Show(id + " " + desc);
            });
        }
        void proc_ProcessCompleted(string id, string desc)
        {
            t.SafeCall(this, delegate
            {
                switch (id)
                {
                    case "GETDTR":
                        proc.Data_Fetch(out dtr);
                        t.MethodCall(GetEmpDtr);
                        break;
                }
            });
        }
        void GetDTR()
        {
            path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\MBB Terminal Data\\dtr.uff";
            UnityScript u = new UnityScript("GETDTR", 5);
            u.Script_Add(Script.LFFPU_READ, path, "elayorbulc");
            proc.Run(u);
        }

        void GetEmpDtr()
        {
            DataTable tmp = dtr.ToDataTable();
            UnityElement dt = new UnityElement(dtr.Desc_Fields());
            DataRow[] rec = tmp.Select("fEmpNo = '" + curID + "'");

            emp = new DataTable();
            emp.Columns.Add("Date");
            emp.Columns.Add("Time-In");
            emp.Columns.Add("Time-Out");

            for (int i = 0; i < rec.Length; i++)
            {
                DataRow dr = emp.NewRow();
                dr[0] = rec[i][1];
                dr[1] = rec[i][2];
                dr[2] = rec[i][3];
                emp.Rows.Add(dr);
            }

            
        }
    }
}
